package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.model.domain.UserLike;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collections;
import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/1 16:49
 */

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询今日佳人
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {

        // 在mongo中查询
        // 构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        // 构建Query对象按照score降序
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")));
        // 进行mongo数据库查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    /**
     * 今日推荐分页查询
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResultVo queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {

        // 构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = new Query(criteria);
        // 总记录数
        long count = mongoTemplate.count(query, RecommendUser.class);
        // 构建query对象，分页查询,降序
        query.limit((page - 1) * pagesize)
                .skip(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        // 分页查询recommendUser表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        // 封装vo分页查询数据
        PageResultVo pageResultVo = new PageResultVo(page, pagesize, (int) count, recommendUsers);
        return pageResultVo;
    }

    /**
     * 佳人详情
     * @param id
     * @param userId
     * @return
     */
    @Override
    public RecommendUser queryByUserId(Long id, Long userId) {

        // 推荐的用户id和当前登录id
        Criteria criteria = Criteria.where("userId").is(id)
                .and("toUserId").is(userId);
        Query query = Query.query(criteria);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 判断查询到的如果是空就构建一个
        if(recommendUser == null){
            recommendUser.setUserId(id);
            recommendUser.setToUserId(userId);
            recommendUser.setScore(99d);
        }

        return recommendUser;
    }

    /**
     * 探花推荐用户列表
     * @param id
     * @param i
     * @return
     */
    @Override
    public List<RecommendUser> queryCardsList(Long id, int i) {

        // 1. 排除喜欢,不喜欢的用户
        // 2. 随机展示
        // 3. 指定数量

        // 查询喜欢不喜欢的用户id
        List<UserLike> userLikeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(id)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikeList, "userId", Long.class);
        // 构建查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(id).and("userId").nin(likeUserIds);
        // 使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(i));
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        List<RecommendUser> mappedResults = results.getMappedResults();
        return mappedResults;
    }
}
